import React from 'react';
import { Router, Route, hashHistory, IndexRedirect, browserHistory,IndexRoute } from 'react-router';
import { Form, Row, Col, Input, Button, Icon, DatePicker, Select, Modal, Upload, message, Table, Tabs, Checkbox } from 'antd';
import App from '../../App';
import ERPfetch from '../../utils/Fetch/ERPfetch';
import Mutil from '../../utils/mm'
import MutilsCookie from '../../utils/cookie.js'
import './login.less'

// 引入存储封装文件
// var __mm = new Mutil();
// 引入加密文件
var forge = require('node-forge');
// 创建md5加密
// var md = forge.md.md5.create();
const FormItem = Form.Item;

class Login extends React.Component{
    constructor(props){
        super(props)
        this.state = {
            data:'',//获取的用户信息密码
            tokenData:[],//获取token数据
            // token:'',
            remember:"",
        }
    }
    handleSubmit = (e) => {
        e.preventDefault();
        this.props.form.validateFields((err, values) => {
            if (!err) {
                // console.log('Received values of form: ', values);
                // if(values.userName == "居居" && values.password == '211314'){
                //         // 请求接口
                //     ERPFetch.post(
                //         '/api/user/login',
                //         {
                //             "appCode": "ERP",
                //             "loginType": "NA",
                //             "password": "e10adc3949ba59abbe56e057f20f883e",
                //             "userLogin": "admin"
                //         },
                //         {
                //             // headers
                //         },
                //     ).then((response) => {
                //         console.log(response)
                //     }).catch((err) => {
                //         console.log(err)
                //     })
                //     browserHistory.push('/app/home')
                // }else{
                //     Modal.error({
                //         title: "用户名或者密码错误，请重新输入"
                //     }); 
                //     // this.props.form.resetFields();
                // }
                this.loginSubmit(values);
            }
            // this.state.data = localStorage.getItem(values.userName);//通过key值获取的value值（密码）
            // 转为JSON格式
            // var msg = JSON.parse(this.state.data)
            // console.log(msg)
            // console.log(this.state.data)
           
        });
        // 登录提交
        // this.setState((state)=>{
        //     state.data
        //     return state;
        // })
        // console.log(this.state.data) 
    }
    loginSubmit=(values)=>{
        // 存储数据
        // localStorage.setItem(values.userName,JSON.stringify(values.password))//存入localstorage(key,value)
        // cookie方式存储
        var passWord = values.password;
        // 装换成小写
        passWord = passWord.toLowerCase();
        // 创建md5加密
        var md = forge.md.md5.create();
        md.update(passWord)
        // 密码加密
        var password = md.digest().toHex();
        // console.log(md.update(values.password),password,values.password)
        console.log(password)
        // this.cookie.set(values.userName,values.password);
        // 请求接口
        ERPfetch.post(
            '/api/user/login',
            {
                "userLogin": values.userName,
                "password": password,
                "loginType": "NA",//NA(用户名),NU(数字账户) ,
                // "appCode": "PRD",//系统模块
            },
            {
                // headers
            },
        ).then((response) => {
            // console.log(response)
            // let tokenData = response.content;
            // this.setState({
            //     tokenData
            // })
            // console.log(tokenData)
            // console.log(response.Code)
            if(response.code == 0){
                console.log("登陆成功")
                // this.cookie.set('userInfo',response.Result.token)
                // this.cookie.get('userInfo')
                // 存token
                Mutil.setStorage('userInfo',response.content)
                // 判断是否为本地
                // let isLocalHost = (window.location.host == 'localhost:3018' ? true : false)
                // console.log('isLocalHost = ' + isLocalHost)
                // 项目交互
                //  console.log(isLocalHost);
                if(window.location.host == 'localhost:3018'){
                    MutilsCookie.set('userInfoDb',response.content.token);
                }else if(window.location.host == 'qaerp.dbuy.cn'){
                    MutilsCookie.setDb('userInfoDb',response.content.token);//测试环境
                }else if(window.location.host == 'erp.dbuy.cn'){
                    MutilsCookie.setPre('userInfo',response.content.token); //正式环境
                }
                // if (isLocalHost) {
                //     MutilsCookie.set('userInfo',response.content.token);
                // } else {
                //     MutilsCookie.setDb('userInfo',response.content.token);
                // }
                // MutilsCookie.set('userInfo',response.content.token)
                // document.cookie='userInfo='+response.content.token
                // console.log(response.content.token)
                browserHistory.push('/app/home')
                // 登录成功保存信息
                // window.localStorage.setItem(values.userName,response.Result.token)
                
                // 取出的事上一个cookie
                // this.state.data = window.localStorage.getItem(values.userName)
                // console.log(this.state.data)
            }
            else if(response.code == 1){
                // Modal.error({
                //     title: response.msg,
                // }); 
                message.warning(response.msg);
                console.log("登陆失败")
            } 
            // 存储数据localstorage
            
        }).catch((err) => {
            console.log(err)
        })
    }
    render() {
        const { getFieldDecorator } = this.props.form;
        const { data, msg } = this.state;
        return (
            <Form onSubmit={this.handleSubmit} className="login-form">
                <span className="loginName">ShangPin</span>
                <FormItem>
                    {getFieldDecorator('userName', {
                        rules: [{ required: true, message: '请输入用户名，用户名不能为空!' }],
                    })(
                        <Input prefix={<Icon type="user" style={{ color: 'rgba(0,0,0,.25)' }} />} placeholder="用户名" />
                    )}
                </FormItem>
                <FormItem>
                    {getFieldDecorator('password', {
                        rules: [{ required: true, message: '请输入密码，密码不能为空!' }],
                    })(
                        <Input prefix={<Icon type="lock" style={{ color: 'rgba(0,0,0,.25)' }} />} type="password" placeholder="密码" />
                    )}
                </FormItem>
                <FormItem>
                    <Button type="primary" onClick={this.handleSubmit} className="login-form-button">
                        登录
                    </Button>
                </FormItem>   
            </Form>
        );
    }
}

const A = Form.create()(Login)
export default A;